package ks.server.controllers;

import java.util.Collection;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.framework.interfaces.IProcessMessage;
import ks.managers.icon.Icon;
import ks.managers.icon.IconManager;
import ks.server.ServerContext;

import org.w3c.dom.Document;

/**
 * Process request to get Icon list.
 * 
 * @author George Heineman
 *
 */
public class IconListController implements IProcessMessage {

	/** Primary agent to whom we can communicate our messages. */
	Communicator com;
	
	public IconListController (Communicator com) {
		this.com = com;
	}
	
	/**
	 * To process the ICON-LIST command we create a request with all known icons.
	 */
	public boolean process(Message m) {

		IconManager im = ServerContext.getIconManager();
		
		Collection<Icon> iconSet = im.getList();
	 
        // produce request containing all ICONS and their respective URLs
        StringBuilder sb = new StringBuilder("<request version='1.0' id='1'><icons>");
        
        for (Icon icon : iconSet) {
        	sb.append("<icon name=\"" + icon.name + "\" uri=\"" + icon.location + "\"/>");
        }
        
        sb.append("</icons></request>");
        Document d = Message.construct(sb.toString());
        
        // send to originating client
        Message r = new Message (d);
        r.setRecipient(m.getOriginator());
        
        return com.distribute(r);
	}
}
